/* Hybrid System Abstractor
 *
 * Copyright (c) 2013, Fortiss GmbH - An-Institut TU Muenchen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fortiss.util;

import java.util.*;
import javax.xml.bind.annotation.*;



@XmlRootElement
public class Predicate {
    @XmlElement
    private Matrix l;
    
    @XmlElement
    private Matrix le;
    
    public Predicate()  {}
    
    public Predicate(double l[][], double le[][])
    {
        if (l!=null) {
            this.l = new Matrix(l);
        }
        if (le!=null){
            this.le = new Matrix(le);
        }
    }
    
    public Matrix getL()  { return l; }
    public Matrix getLE()  { return le; }
    
    
    public String toString(List<String> var)
    {
        String ret = toString(l, var, "<", "");
        ret = toString(le, var, "<=", ret);
        return ret;
    }

    private String toString(Matrix m, List<String> var, String op, String ret)
    {
        for (int i=0; m!=null && i<m.getRow(); i++){
            String f = "";
            for (int j=0; j<m.getColumn()-1; j++){
                String term = "";
                if (m.getElement(i, j)<0)      term = "(" + m.getElement(i, j) + ")" + var.get(j);
                else if (m.getElement(i, j)>0) term = m.getElement(i, j) + var.get(j);
                
                if (! term.equals("")){
                    if (f.equals("")) f = term;
                    else f+= "+" + term;
                }
            }
            if (f.equals("")) f = "0 " + op + " " + m.getElement(i, m.getColumn()-1);
            else              f += " " + op + " " + m.getElement(i, m.getColumn()-1);
            if (ret.equals("")) ret = f;
            else ret += ", " + f;
        }
        return ret;
    }
    
    public int getVariable()
    {
        if (l!=null)  return l.getColumn()-1;
        if (le!=null) return le.getColumn()-1;
        return 0;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.l != null ? this.l.hashCode() : 0);
        hash = 79 * hash + (this.le != null ? this.le.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Predicate other = (Predicate) obj;
        if (this.l != other.l && (this.l == null || !this.l.equals(other.l))) {
            return false;
        }
        if (this.le != other.le && (this.le == null || !this.le.equals(other.le))) {
            return false;
        }
        return true;
    }
}
